/**
 * 
 */
package co.fitstart.entity.order;

import java.util.Date;

import com.demo2do.core.utils.DateFormatUtils;

import co.fitstart.entity.product.ProductType;
import co.fitstart.entity.reference.Contact;

/**
 * @author downpour
 *
 */
public class Shopping {
    
    /** 下单用户 */
    private Long userId;
    
    private Contact contact;
    
    /** 下单产品 */
    private Long productId;
    
    private Long siteId;
    
    /** 购买数量 */
    private int amount;
    
    /** 优惠券 */
    private Long couponId;
    
    /** 产品类型 */
    private String type;
    
    /** 用户留言 */
    private String comment;
    
    /**
     * 
     * @param userId
     * @return
     */
    public Shopping user(Long userId) {
        this.userId = userId;
        return this;
    }

    /**
     * 
     * @return
     */
    public Order createOrder() {
        return new Order(amount);
    }
    
    /**
     * 
     * @return
     */
    public String createOrderNo() {
        
        // TODO add header sign
        
        // 2 digits random number between 0 (inclusive) ~ 100 (exclusive)
        String randomNumber = String.format("%02d", Double.valueOf(Math.floor(Math.random() * 100)).intValue());
        
        // Generate code with 20 digits total
        return new StringBuilder().append("W")
                                  .append(randomNumber)
                                  .append(DateFormatUtils.format(new Date(), "yyMMddHHmmssSSS"))
                                  .toString();
    }
    
    /**
     * 
     * @return
     */
    public boolean withCoupon() {
        return this.couponId != null;
    }
    
    /**
     * 
     * @return
     */
    public boolean withSite() {
        return this.siteId != null;
    }
    
    /**
     * 
     * @return
     */
    public boolean withType() {
        ProductType productType = ProductType.aliasOf(this.type);
        return productType == null ? false : true;
    }
    
    /**
     * 
     * @return
     */
    public OrderType getOrderType() {
        OrderType orderType = OrderType.aliasOf(this.type);
        return orderType;
    }

    /**
     * @return the userId
     */
    public Long getUserId() {
        return userId;
    }

    /**
     * @return the contact
     */
    public Contact getContact() {
        return contact;
    }

    /**
     * @return the productId
     */
    public Long getProductId() {
        return productId;
    }

    /**
     * @return the siteId
     */
    public Long getSiteId() {
        return siteId;
    }

    /**
     * @return the amount
     */
    public int getAmount() {
        return amount;
    }

    /**
     * @return the couponId
     */
    public Long getCouponId() {
        return couponId;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @return the comment
     */
    public String getComment() {
        return comment;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(Long userId) {
        this.userId = userId;
    }

    /**
     * @param contact the contact to set
     */
    public void setContact(Contact contact) {
        this.contact = contact;
    }

    /**
     * @param productId the productId to set
     */
    public void setProductId(Long productId) {
        this.productId = productId;
    }

    /**
     * @param siteId the siteId to set
     */
    public void setSiteId(Long siteId) {
        this.siteId = siteId;
    }

    /**
     * @param amount the amount to set
     */
    public void setAmount(int amount) {
        this.amount = amount;
    }

    /**
     * @param couponId the couponId to set
     */
    public void setCouponId(Long couponId) {
        this.couponId = couponId;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @param comment the comment to set
     */
    public void setComment(String comment) {
        this.comment = comment;
    }
}
